package com.tc.repositories;

import com.tc.config.SpringDataJPAConfig;
import com.tc.pojo.Customer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @Auther: tianchao
 * @Date: 2021/12/4 01:24
 * @Description:
 */
@ContextConfiguration(classes = SpringDataJPAConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class CustomerRepositoryTestConfig {
    @Autowired
    private CustomerRepository customerRepository;
    @Test
    public void testR(){
        Optional<Customer> customer = customerRepository.findById(1L);
        System.out.println(customer.get());
    }
    @Test
    public void testCU(){
        Customer customer = new Customer();
        //customer.setCustId(8L);
        customer.setCustName("data-JPA-u");
        customer.setCustAddress("spring");
        Customer save = customerRepository.save(customer);
        System.out.println(save);
    }
    @Test
    public void testD(){
        Customer customer = new Customer();
        customer.setCustId(7L);
        customer.setCustName("张三");// name没生效 查询没带 但是避免了状态
        customerRepository.delete(customer);
    }
    @Test
    public void testSaveAll(){
        Customer customer = new Customer();
        customer.setCustName("aa");// name没生效 查询没带 但是避免了状态
        Customer customer2 = new Customer();
        customer2.setCustName("bb");// name没生效 查询没带 但是避免了状态
        Customer customer3 = new Customer();
        customer3.setCustId(1L);
        customer3.setCustName("田超u");// name没生效 查询没带 但是避免了状态
        List<Customer> customers = Arrays.asList(customer, customer2,customer3);
        Iterable<Customer> result = customerRepository.saveAll(customers);
        result.forEach(System.out::println);
    }

    /**
     * 查询一个没有会报错
     */
    @Test
    public void testFindById(){
        Optional<Customer> byId = customerRepository.findById(1L);
        System.out.println(byId.get());
    }
    /**
     * 查询一个没有会报错
     */
    @Test
    public void testFindAllById(){
        List<Long> longs = Arrays.asList(3L, 2L);
        Iterable<Customer> allById = customerRepository.findAllById(longs);
        allById.forEach(System.out::println);
    }
}
